package com.housesales.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URISyntaxException;
import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.Map;
import java.util.Timer;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.log4j.Logger;

import com.housesales.thread.MyTimerTask;

import utils.PropertyUtil;

/**
 * 
* @ClassName: TheardServlet
* @Description: 线程SERVLET
* @author chenchao
* @date 2013-12-12 上午02:00:25
*
 */
public class TheardServlet  extends HttpServlet {
	private static Logger logger = Logger.getLogger(Thread.class.getName());
	 private static long PERIOD_DAY ;
	/* (non-Javadoc)
	 * @see javax.servlet.GenericServlet#init(javax.servlet.ServletConfig)
	 */
	@Override
	public void init(ServletConfig config) throws ServletException {
		String classPath = config.getInitParameter("propertiesFile");
		long hour = Long.parseLong(config.getInitParameter("hour"));
		long mintue = Long.parseLong(config.getInitParameter("minitue"));
		long seconde = Long.parseLong(config.getInitParameter("seconde"));
		long minseconde = Long.parseLong(config.getInitParameter("miniseconde"));
		int startHour = Integer.parseInt(config.getInitParameter("startHour"));
		int startMinitue = Integer.parseInt(config.getInitParameter("startMinitue"));
		int startSeconde = Integer.parseInt(config.getInitParameter("startSeconde"));
		PERIOD_DAY =  hour * mintue * seconde * minseconde;
		logger.debug("init");
		try {
			StringBuilder sb = new StringBuilder();
			
			sb = packFilePath(classPath, sb);
			Map<String,String> propertyMap = PropertyUtil.getPropertiesData(new FileInputStream(new File(
					TheardServlet.class.getResource(
							sb.toString()).toURI())));
			logger.debug(propertyMap.get("overdue"));
	        Timer timer = new Timer();  
	        MyTimerTask tt = new MyTimerTask(Integer.parseInt(propertyMap.get("overdue")));
	        Date date = calculateDay(startHour,startMinitue,startSeconde);
	        timer.schedule(tt,date,PERIOD_DAY);    
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		} catch (URISyntaxException e) {
			e.printStackTrace();
			logger.error(e.getMessage());
		}

	}
	
	/**
	 * 计算日期
	 * @return
	 */
	private Date calculateDay(int startHour,int startMinitue,int startSeconde) {
		  Calendar calendar = Calendar.getInstance();  
	        calendar.set(Calendar.HOUR_OF_DAY, startHour); //凌晨1点  
	        calendar.set(Calendar.MINUTE, startMinitue);  
	        calendar.set(Calendar.SECOND, startSeconde);  
	        Date date=calendar.getTime(); 
	        //第一次执行定时任务的时间  
	        //如果第一次执行定时任务的时间 小于当前的时间  
	        //此时要在 第一次执行定时任务的时间加一天，以便此任务在下个时间点执行。如果不加一天，任务会立即执行。  
	        if (date.before(new Date())) {  
	            date = this.addDay(date, 1);  
	        }  
	        
	        return date;

	}
	
	 // 增加或减少天数  
    private Date addDay(Date date, int num) {  
        Calendar startDT = Calendar.getInstance();  
        startDT.setTime(date);  
        startDT.add(Calendar.DAY_OF_MONTH, num);  
        return startDT.getTime();  
    }  
	
	
	/**
	 * 组装路径
	 * @param classPath
	 * @param sb
	 * @return
	 */
	private StringBuilder packFilePath(String classPath,StringBuilder sb) {
		if (classPath.indexOf("classpath:") != -1){
			sb.append("/");
			sb.append(classPath.split(":")[1]);
		}
		return sb;
	}
	
	
}
